#! /bin/bash

AUTH='67890'
PROJECT_NAME='gitbooks'
SITE_PATH=`echo $PWD`
PROJECT_PATH=${SITE_PATH}/${PROJECT_NAME}
ONE_PATH=${SITE_PATH}/${PROJECT_NAME}/ones
DEPLOY='deploy.lock'
ADD_PATH="${SITE_PATH}/var/"
ADD_FILE='gitbook.add'
ADDED_FILE='gitbook.added'
REVIEW_FILE='gitbook.review'
BOOK_JSON='book.json'

USER='www-data'
USERGROUP='www-data'


add_books() {
    if [ ! -f ${ADD_PATH}/${ADD_FILE} ];then
        return 1;
    fi
    echo "Start add new books"
    while read line
    do
        echo "Start add new book : $line"
        git=`echo $line | awk -F'-_-' '{print $1}'`
        type=`echo $line | awk -F'-_-' '{print $2}'`
        category=`echo $line | awk -F'-_-' '{print $3}'`
        authority=`echo $line | awk -F'-_-' '{print $4}'`
        email=`echo $line | awk -F'-_-' '{print $5}'`

        if [[ -n "$authority" ]] || [[ "$authority" -ne "$AUTH" ]];then
            echo "Need to review the book : $git"
            echo $line >> ${ADD_PATH}/${REVIEW_FILE}
            continue;
        fi

        book=`echo ${git%.*} | awk -F'/' '{print $5}'`
        if [ ! -n "$book" ]; then
            echo "error book git : ${git}"
            continue;
        fi
        if [ "$type" -eq "1" ];then
            cd $ONE_PATH
            if [ -d "${book}" ];then
                echo "Exist the book : ${ONE_PATH}/${book}"
                continue;
            fi
            git clone $git;sleep 1
            if [ ! -d "${book}" ];then
                echo "Not git pull the book : ${ONE_PATH}/${book}"
                continue;
            fi
            cd $book

            if [ ! -f "${BOOK_JSON}" ];then
                echo "Not find the ${BOOK_JSON}"
                continue;
            fi

            docker run --rm --volume $PWD:/gitbook gitbook  bash -c 'gitbook install --registry=https://registry.npm.taobao.org && gitbook build'
            if [ -d "${SITE_PATH}/public/books/$book" ];then
                rm -rf ${SITE_PATH}/public/books/$book
            fi
            mv _book ${SITE_PATH}/public/books/$book
            echo $line >> ${ADD_PATH}/${ADDED_FILE}
            cd ${PROJECT_PATH}
            echo ""
        else
            cd ${PROJECT_PATH}
            if [ -d "${book}" ];then
                echo "Exist the book : ${PROJECT_PATH}/${book}"
                continue;
            fi
            git clone $git;sleep 1
            if [ ! -d "${book}" ];then
                echo "Not git pull the book : ${ONE_PATH}/${book}"
                continue;
            fi
            cd $book

            for sub_book in `ls -d */`
            do
                if [ -d "${SITE_PATH}/public/books/$sub_book" ];then
                    echo "Exist the book : ${PROJECT_PATH}/${book}/${sub_book}"
                    continue;
                fi
                cd $sub_book
                if [ ! -f "${BOOK_JSON}" ];then
                    echo "Not find the ${BOOK_JSON}"
                    continue;
                fi
                docker run --rm --volume $PWD:/gitbook gitbook  bash -c 'gitbook install --registry=https://registry.npm.taobao.org && gitbook build'
                mv _book ${SITE_PATH}/public/books/$sub_book
                cd ../
                echo ""
            done
            echo $line >> ${ADD_PATH}/${ADDED_FILE}
        fi
    done < ${ADD_PATH}/${ADD_FILE}
    rm ${ADD_PATH}/${ADD_FILE}
    echo "End add new books"
}

build_ones() {
    cd $ONE_PATH
    ls -d */ > /dev/null 2>&1
    if [ "$?" -ne "0" ];then
        return 1;
    fi
    for i in `ls -d */`
    do
        cd $i
        is_deploy=`cat ${DEPLOY} 2> /dev/null`
        if [[ $? -ne 1 ]] && [[ "${is_deploy}" -eq "1" ]];then
            echo "Start build $ONE_PATH/$i"
            git reset --hard origin/master
            git clean -f
            git pull > .gitchange 2>&1
            git checkout master
            cat .gitchange | grep 'Updating' > /dev/null
            if [ $? -eq 1 ];then
                echo "$i not changed"
                continue;
            fi
            echo "$i has changed..."
            docker run --rm --volume $PWD:/gitbook gitbook  gitbook build

            if [ -d "${SITE_PATH}/public/books/$i" ];then
                rm -rf ${SITE_PATH}/public/books/$i
            fi
            mv _book ${SITE_PATH}/public/books/$i
            echo "End build $ONE_PATH/$i"
        fi
    done
}

build_books() {
    cd ${PROJECT_PATH}
    for i in `ls -d */ `
    do
        if [ "$i" == "ones/" ];then
            continue;
        fi
        cd $i
        is_deploy=`cat ${DEPLOY} 2> /dev/null`
        if [[ $? -ne 1 ]] && [[ "${is_deploy}" -eq "1" ]];then
            echo "Start build other books"
            git reset --hard origin/master
            git clean -f
            git pull > .gitchange 2>&1
            git checkout master

            for j in `ls -d */`
            do
                cat .gitchange | grep $j > /dev/null
                if [ $? -eq 1 ];then
                    echo "$j not changed"
                    continue;
                fi
                echo "$j has changed..."
                cd $j
                docker run --rm --volume $PWD:/gitbook gitbook  gitbook build

                if [ -d "${SITE_PATH}/public/books/$j" ];then
                    rm -rf ${SITE_PATH}/public/books/$j
                fi
                mv _book ${SITE_PATH}/public/books/$j

                cd ../
            done
            git clean -f
            cd $PROJECT_PATH
            echo "End build other books"
        fi
    done

}

while true
do
    add_books
    build_ones
    build_books
    cd $SITE_PATH
    chown -R $USER:$USERGROUP public/books
    chmod -R 755 public/books
    sleep 1
done
